<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 17/12/12
 * Time: 15:41
 */

namespace App\Service;

use App\Http\Controllers\Admin\Finance\Repository\Account as AccountRepository;
use App\Http\Controllers\Admin\Finance\Repository\Commission;
use App\Http\Controllers\Admin\Finance\Repository\Withdraw;

class Account
{
    private $repository;
    private $member_id;
    private $type;

    const DISTRIBUTE =   1; //推荐奖
    const REBATE = 2;   //下级消费返利
    const FILL_INFORMATION = 3; //完善资料奖励
    const WITHDRAW = 4; //提现
    const RECHARGE_BY_COMBO = 5;    //套餐、活动充值
    const RECHARGE_BY_ADMIN = 6;    //后台充值
    const BUY_SCENE = 7;    //景点购买
    const REBATE_TOP = 8;   //团队奖（团队消费返利）
    const WITHDRAW_REFUSE = 9;//提现拒绝返回申请款


    const RECHARGE_ADMIN = 1;//后台充值
    const RECHARGE_COMBO = 2;//套餐充值
    const RECHARGE_ACTIVITY = 3;//活动充值

    public function __construct ()
    {
        $this->repository = new AccountRepository();
    }
    public function account_log($member_id,$type,$money,$options=[]){
        $desc = '';
        switch ($type){
            case self::DISTRIBUTE:
                $desc = '推广奖励';
                $this->distribute($options['from'],$member_id,$money,$type,$desc);
                break;
            case self::REBATE:
                $desc = '下级用户消费奖励';
                $this->distribute($options['from'],$member_id,$money,$type,$desc);
                break;
            case self::FILL_INFORMATION:
                $desc = '完善信息奖励('.config('wallet.wallet.wallet_spots').')';
                break;
            case self::WITHDRAW:
                $desc = '提现扣除'.config('wallet.wallet.wallet_push_money');
                $this->withdraw($member_id,$money,$options['needApprove']);
                break;
            case self::RECHARGE_BY_COMBO:
                $desc = '用户购买套餐';
                $desc .= '充值'.config('wallet.wallet.wallet_spots');
                break;
            case self::RECHARGE_BY_ADMIN:
                $desc = '后台充值'.config('wallet.wallet.wallet_spots');
                break;
            case self::BUY_SCENE:
                $desc = '浏览景点消耗'.config('wallet.wallet.wallet_spots');
                break;
            case self::REBATE_TOP:
                $desc = '团队消费奖励';
                $this->distribute($options['from'],$member_id,$money,$type,$desc);
                break;

            case self::WITHDRAW_REFUSE:
                $desc = '提现拒绝返回提成';
                break;

        }
        $model = $this->repository;
        $model->member_id = $member_id;
        $model->type = $type;
        $model->price = $money;
        $model->desc = $desc;
        return $model->save();
    }
    private function distribute($from,$to,$money,$type=self::DISTRIBUTE,$desc='推广奖励'){
        $model = new Commission();
        $model->from_member = $from;
        $model->to_member = $to;
        $model->price = $money;
        $model->type = $type;
        $model->desc = $desc;
        return $model->save();
    }
    private function withdraw($member,$money,$needApprove=false){
        $model = new Withdraw();
        $model->type = 2;
        $model->price = $money;
        $model->member_id = $member;
        $model->status = $needApprove?1:5;
        $model->apply_time = date('Y-m-d H:i:s',time());
    }
}